package com.jds.test.bio.p6;

/**
 *
 * https://www.cnblogs.com/silyvin/articles/12019528.html
 * https://www.cnblogs.com/silyvin/articles/12037917.html
 * https://www.cnblogs.com/silyvin/articles/12041638.html
 * https://www.cnblogs.com/silyvin/articles/12057673.html
 * Created by joyce on 2019/11/26.
 */


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;


public class Client {

    public static final int PORT = 12123;
    public static final int BUFFER_SIZE = 100; // 100 500 1000 1300

    public static void main(String []f) throws IOException {
        new Client().client();
    }

    //客户端代码
    public void client() throws UnknownHostException, IOException{

        final String s1 = "49.235.75.155";// tx
        final String s2 = "localhost";
        final String s3 = "39.100.99.222";// al


    //    Socket s = new Socket(s1,PORT);//创建socket连接
        Socket s = new Socket();
        System.out.println(s.getReceiveBufferSize());
        System.out.println(s.getSendBufferSize());
        s.setReceiveBufferSize(100);

        /**
         * 目的
         * 1 使其有包就发，避免在nagle作用下发送端沾包，若没有这句使用默认并启用nagle，则沾包
         * 而且不会阻塞，100个write会成功返回，虽然只发出去1152个字节
         * 2 与BUFFER_SIZE相同，暂时不考虑发送缓冲区拆包
         */
        s.setSendBufferSize(BUFFER_SIZE);

        /**
         * 禁用nagle，可连续发包
         */
     //   s.setTcpNoDelay(true);

        /**
         * 重要参数
         */
//        System.out.println(s.getSoLinger());
//        s.setSoLinger(true, 0);

        s.connect(new InetSocketAddress(s1, PORT));

        if(BUFFER_SIZE == 1300) {
            s.getOutputStream().write(new byte[500]);   // 100 500 1000
        }

        for(int i=0; i<100; ++i) {
            s.getOutputStream().write(new byte[BUFFER_SIZE]);
            System.out.println("send + " + i);
        }

    }
}